package com.oursnail.jms.topic;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * Created By XJL On 2017/11/4
 */
public class AppProducer {

    private static final String url="tcp://localhost:61616";
    private static final String topicName="query-test";

    public static void main(String[] args) throws JMSException{
    //1、创建连接工厂
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
        //2、创建connection
        Connection connection = connectionFactory.createConnection();
        //3、启动连接
        connection.start();
        //4、创建会话
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        //5、创建目的地
        Destination destination = session.createTopic(topicName);
        //6、创建一个生产者
        MessageProducer messageProducer = session.createProducer(destination);
        for (int i = 0;i<100;i++){
            //7、创建消息
            TextMessage textMessage = session.createTextMessage("Test:"+i);
            //8、发布消息
            messageProducer.send(textMessage);
            System.out.println("  发送消息："+textMessage.getText());
        }
        //9、连接关闭
        connection.close();
    }
}
